package com.hzw.saas.web.app.group.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hzw.saas.api.group.IGroupService;
import com.hzw.saas.api.group.bo.GroupBo;
import com.hzw.saas.api.group.bo.GroupMemberRoleBo;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.common.security.utils.SecurityUtils;
import com.hzw.saas.common.util.MapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author sonam
 * @since 2020/12/31 9:09
 */
@RestController
@AllArgsConstructor
@Slf4j
@Api(tags = "群组管理/本人群组")
public class UserGroupController {

    private final IGroupService groupService;

    @GetMapping("/user/groups/count")
    @ApiOperation(value = "统计用户所在群组数量", notes = "统计用户所在群组数量，包括自己创建的，加入别人的")
    @ApiResponse(code = 200, message = "本人所在群组数量", response = Integer.class)
    public ResponseEntity<Integer> countAllGroup() {
        PageParam pageParam = new PageParam(1, 1);
        IPage<GroupMemberRoleBo> allGroupAndOther = groupService.getAllGroupAndOther(pageParam.convert(), SecurityUtils.getUser().getUserId());
        return ResponseEntity.ok((int) allGroupAndOther.getTotal());
    }

    @GetMapping("/user/groups")
    @ApiOperation(value = "获取本人所有群组信息", notes = "获取本人的所有群组列表，包括自己创建的，加入别人的")
    @ApiResponses({
        @ApiResponse(code = 200, message = "true")
    })
    public ResponseEntity<IPage<GroupMemberRoleBo>> getAllGroup(PageParam pageParam) {
        /**
         * 1、获得所有自己所属的群组信息，包括角色
         */
        IPage<GroupMemberRoleBo> groupDtoList = groupService.getAllGroupAndOther(pageParam.convert(), SecurityUtils.getUser().getUserId());

        List<GroupMemberRoleBo> records = groupDtoList.getRecords();
        if(CollectionUtils.isEmpty(records)) groupDtoList.setRecords(new ArrayList<>());

        if(pageParam == null || pageParam.getCurrent() == 1) {
            List<GroupBo> groupDefaultDtoList = groupService.getDefaultGroupListByUserId(SecurityUtils.getUser().getUserId());
            if (CollectionUtils.isNotEmpty(groupDefaultDtoList)) {
                List<GroupMemberRoleBo> extraList = new ArrayList<>();

                groupDefaultDtoList.forEach(groupDefaultDto -> {
                    // 添加自己
                    GroupMemberRoleBo mro = MapperUtil.nf().map(groupDefaultDto, GroupMemberRoleBo.class);
                    mro.setMemberId("1");
                    mro.setJoinDate(new Date());
                    mro.setRoleId("2");
                    mro.setRoleName("成员");
                    extraList.add(mro);
                });
                groupDtoList.getRecords().addAll(0, extraList);

                groupDtoList.setTotal(groupDtoList.getTotal() + groupDefaultDtoList.size());
            }
        }

        return ResponseEntity.ok(groupDtoList);
    }

}
